﻿Public Class frmQLLoaiPhong

    Dim connect As New connect()
    Private Sub frmQLLoaiPhong_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadLP()
    End Sub
    Private Sub LoadLP()
        dgvLoaiPhong.ClearSelection()
        Dim LoaiPhongBUS = New LoaiPhongBUS()
        Dim LoaiPhongList As New List(Of LoaiPhongDTO)
        LoaiPhongList = LoaiPhongBUS.GetData()
        dgvLoaiPhong.DataSource = LoaiPhongList
        dgvLoaiPhong.ClearSelection()
    End Sub
    Private Sub LamMoi()
        LoadLP()
        dgvLoaiPhong.ClearSelection()
        btnInsert.Enabled = True
        btnUpdate.Enabled = False
        btnDelete.Enabled = False
        txtMaLP.Text = ""
        txtMaLP.Enabled = True
        txtTenLP.Text = ""
        cbBiXoa.Checked = False
    End Sub
    Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click
        If (txtMaLP.Text <> "") Then
            Dim sql As String = "update LOAIPHONG set TENLP=N'" + txtTenLP.Text + "', BIXOA='" + cbBiXoa.Checked.ToString() + "' where MALP='" + txtMaLP.Text + "'"
            connect.ExecSql(sql)
            LoadLP()
        End If
    End Sub

    Private Sub dgvLoaiPhong_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles dgvLoaiPhong.SelectionChanged
        txtMaLP.Text = dgvLoaiPhong.CurrentRow.Cells(0).Value.ToString().Trim()
        txtTenLP.Text = dgvLoaiPhong.CurrentRow.Cells(1).Value.ToString().Trim()
        cbBiXoa.Checked = Convert.ToBoolean(dgvLoaiPhong.CurrentRow.Cells(2).Value.ToString())
        btnUpdate.Enabled = True
        btnDelete.Enabled = True
        btnInsert.Enabled = False
        txtMaLP.Enabled = False
    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        LamMoi()

    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        Dim dt As New DataTable

        dt = connect.GetData("select MALP from TINDANG where MALP=" + txtMaLP.Text.Trim())
        If (dt.Rows.Count() >= 1) Then
            MessageBox.Show("Loại này đang có dữ liệu. Tôi sẽ đánh dấu xoá giúp bạn")
            connect.ExecSql("update LOAIPHONG set BIXOA='True' where MALP=" + txtMaLP.Text)
            LamMoi()
        Else
            connect.ExecSql("delete LOAIPHONG where MALP=" + txtMaLP.Text)
            MessageBox.Show("Xoá thành công!")
        End If
        LamMoi()
    End Sub

    Private Sub btnInsert_Click(sender As System.Object, e As System.EventArgs) Handles btnInsert.Click
        Dim dt As New DataTable
        dt = connect.GetData("select MALP from LOAIPHONG where MALP=" + txtMaLP.Text.Trim())
        If (dt.Rows.Count() >= 1) Then
            MessageBox.Show("Mã loại phòng này đã tồn tại")
            txtMaLP.Focus()
            Return
        Else
            Dim sql As String = "insert into LOAIPHONG(MALP,TENLP,BIXOA) values (" + txtMaLP.Text.Trim() + ",N'" + txtTenLP.Text.Trim() + "','" + cbBiXoa.Checked.ToString() + "')"
            connect.ExecSql(sql)
            MessageBox.Show("Thêm thành công!")
            LamMoi()
        End If
    End Sub
End Class